PowerTCP Mail for .NET
BeginSend(Byte[]) Method
See Also  Example Send comments on this topic.
Dart.PowerTCP.Mail Namespace > Tcp Class > BeginSend Method : BeginSend(Byte[]) Method




buffer
Source memory location for the data to send.
Send data from your buffer asynchronously. The Tcp.EndSend event is raised when completed.

Syntax

Visual Basic (Declaration) 
<DescriptionAttribute("Send data from your buffer asynchronously. The EndSend event is raised when completed.")>
Public Overloads Function BeginSend( _
   ByVal buffer() As Byte _
) As IAsyncResult
Visual Basic (Usage)Copy Code
Dim instance As Tcp
Dim buffer() As Byte
Dim value As IAsyncResult
 
value = instance.BeginSend(buffer)
C# 
[DescriptionAttribute("Send data from your buffer asynchronously. The EndSend event is raised when completed.")]
public IAsyncResult BeginSend( 
   byte[] buffer
)
Managed Extensions for C++ 
[DescriptionAttribute("Send data from your buffer asynchronously. The EndSend event is raised when completed.")]
public: IAsyncResult* BeginSend( 
   byte[]* buffer
) 
C++/CLI 
[DescriptionAttribute("Send data from your buffer asynchronously. The EndSend event is raised when completed.")]
public:
IAsyncResult^ BeginSend( 
   array<byte>^ buffer
) 

Parameters

buffer
Source memory location for the data to send.

Return Value

An IAsyncResult that represents the asynchronous operation, which could still be pending.

Exceptions

ExceptionDescription
System.InvalidOperationExceptionBeginXXX method used without providing an EndXXX event handler.
System.ArgumentOutOfRangeExceptionoffset or count is less than 0.
System.ArgumentExceptionoffset + count is greater than the length of buffer.
System.Net.Sockets.SocketExceptionThe socket is not connected.

Example

The following example demonstrates asynchronous receiving and sending of data.
Visual BasicCopy Code
Private Sub AsynchTcpDemo()

   ' Attempt to connect to an echo port.
   Try
      Tcp1.Connect("myserver", 7)
   Catch ex As Exception
      Return
   End Try

   ' Send data, since we are connected to an echo port, the same data should be returned.
   ' The following code demonstrates asynchronously sending data. When data has been sent
   ' the EndSend event will be raised.

   Dim buffer() As Byte = System.Text.Encoding.Default.GetBytes("a")
   Tcp1.BeginSend(buffer, 0, buffer.Length, Net.Sockets.SocketFlags.None, Nothing)
End Sub

Private Sub Tcp1_EndSend(ByVal sender As Object, ByVal e As Dart.PowerTCP.SegmentEventArgs) Handles Tcp1.EndSend
   ' Check for exception
   If e.Exception Is Nothing Then

      ' Send is complete. Display info about the data sent.
      Debug.WriteLine("Byte count sent: " + e.Segment.Count)
      Debug.WriteLine("Data sent: " + e.Segment.ToString())
      
      Dim buffer(Tcp1.ReceiveBufferSize) As Byte

      ' Receive the data. The EndReceive event will be raised upon completion.
      Tcp1.BeginReceive(buffer, 0, buffer.Length, Net.Sockets.SocketFlags.None, Nothing)
    
    End If
End Sub

Private Sub Tcp1_EndReceive(ByVal sender As Object, ByVal e As Dart.PowerTCP.SegmentEventArgs) Handles Tcp1.EndReceive
   ' Check for exception
   If e.Exception Is Nothing Then

      ' Receive is complete. Display info about the data sent.
      Debug.WriteLine("Byte count received: " + e.Segment.Count)
      Debug.WriteLine("Data received: " + e.Segment.ToString())

      ' Close the connection
      Tcp1.Close()
    End If
End Sub
C#Copy Code
private void AsynchTcpDemo()
{
    // Attempt to connect to an echo port.
    try
    {
        tcp1.Connect("myserver", 7);
    }
    catch(Exception ex)
    {return;}

    // Send data, since we are connected to an echo port, the same data should be returned.
    // The following code demonstrates asynchronously sending data. When data has been sent
    // the EndSend event will be raised.

    byte[] buffer = System.Text.Encoding.Default.GetBytes("a");
    tcp1.BeginSend(buffer, 0, buffer.Length, System.Net.Sockets.SocketFlags.None, null);
}

private void tcp1_EndSend(object sender, Dart.PowerTCP.SegmentEventArgs e)
{
    // Check for exception
    if(e.Exception == null)
    {
        // Send is complete. Display info about the data sent.
        Debug.WriteLine("Byte count sent: " + e.Segment.Count);
        Debug.WriteLine("Data sent: " + e.Segment.ToString());
                

        byte[] buffer = new byte[tcp1.ReceiveBufferSize];
                    
        // Receive the data. The EndReceive event will be raised upon completion.
        tcp1.BeginReceive(buffer, 0, buffer.Length, System.Net.Sockets.SocketFlags.None, null);
    }
}

private void tcp1_EndReceive(object sender, Dart.PowerTCP.SegmentEventArgs e)
{
    // Check for exception
    if(e.Exception == null)
    {
        // Receive is complete. Display info about the data sent.
        Debug.WriteLine("Byte count received: " + e.Segment.Count);
        Debug.WriteLine("Data received: " + e.Segment.ToString());

        // Close the connection
        tcp1.Close();
    }
}

Remarks

The Tcp.BeginSend method is used to send data from a buffer asynchronously. When the data has been sent, the Tcp.EndSend event is raised. A SegmentEventArgs object is passed into this event, containing a Segment object, containing information about the data sent, including the data itself and the amount of bytes of data sent.

If you are using the Tcp component as a reference, you must "wire up" the event yourself. This involves creating a method as the event handler that implements the SegmentEventHandler delegate.

To synchronously send data, use the Tcp.Send method.

Use this method if you wish to receive data into a buffer transparently with minimal application impact, as execution occurs on another thread. This method is the standard means for asynchronously sending data.

Requirements

Target Platforms: Microsoft .NET Framework 2.0

See Also

Documentation Version 3.2
© 2010 Dart Communications. All Rights Reserved.